package org.xianxin.baidu.ocr.demo;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import sun.net.www.http.HttpClient;

import java.io.*;
import java.util.HashMap;

/**
 * @author 贤心i
 * @version V1.0
 * @Title:
 * @Package org.xianxin.baidu.ocr.demo
 * @Description: (用一句话描述该文件做什么)
 * @date 2019/4/8 13:58
 **/
public class Sample {

    //设置APPID/AK/SK
    /**
     * 你的 App ID
     */
    public static final String APP_ID = "";
    /**
     * 你的 Api Key
     */
    public static final String API_KEY = "FFvrW2oLciu6SXUTDQDfIiaO";
    /**
     * 你的 Secret Key
     */
    public static final String SECRET_KEY = "AG0GpgXDyeZtbRkoSNBjy2YYvqhkkhTR";

    public static void main(String[] args) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        // 设置http代理
        // client.setHttpProxy("proxy_host", proxy_port);
        // 设置socket代理
        // client.setSocketProxy("proxy_host", proxy_port);

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
//        String path = "C:\\Users\\HL\\Desktop\\operatVerificat.jpg";
//        String path = "C:\\Users\\HL\\Desktop\\getCaptchaImage.jpg";
//        String path = "C:\\Users\\HL\\Desktop\\getCaptchaImage (1).jpg";
//        String path = "C:\\Users\\HL\\Desktop\\getCaptchaImage (2).jpg";
//        String path = "C:\\Users\\HL\\Desktop\\1.jpg";
        String path = "C:\\Users\\HL\\Desktop\\captcha\\10.jpg";
//        String path = "C:\\Users\\HL\\Desktop\\5f462bf7aec51ac3496d447854ed99e.png";
//        String path = "C:\\Users\\HL\\Desktop\\e9eea3ef377e9f225eb78dcc71c240c.png";
        JSONObject res = client.basicGeneral(path, new HashMap<>());
        System.out.println("通用文字识别:" + res.toString(2));

        HashMap<String, String> options = new HashMap<>();
        options.put("detect_direction", "true");
        options.put("probability", "true");
        JSONObject res2 = client.basicAccurateGeneral(path, options);
        System.out.println("通用文字识别（高精度版）:" + res2.toString(2));

        // 传入可选参数调用接口
        HashMap<String, String> options2 = new HashMap<>();
        options.put("recognize_granularity", "big");
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("vertexes_location", "true");
        options.put("probability", "true");
        JSONObject res3 = client.general(path, options2);
        System.out.println("通用文字识别（含位置信息版）：" + res3.toString(2));


        // 传入可选参数调用接口
        HashMap<String, String> options3 = new HashMap<>();
        options.put("recognize_granularity", "big");
        options.put("detect_direction", "true");
        options.put("vertexes_location", "true");
        options.put("probability", "true");

        JSONObject res4 = client.accurateGeneral(path, options3);
        System.out.println("通用文字识别（含位置高精度版）：" + res4.toString(2));

        // 传入可选参数调用接口
        HashMap<String, String> options4 = new HashMap<>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
        JSONObject res5 = client.enhancedGeneral(path, options4);
        System.out.println("通用文字识别（含生僻字版）：" + res5.toString(2));

        // 传入可选参数调用接口
        HashMap<String, String> options5 = new HashMap<>();
        options.put("detect_direction", "true");
        options.put("detect_language", "true");

        JSONObject res6 = client.webImageUrl("http://issue.cpic.com.cn/ecar/auth/getCaptchaImage?0.9071533982873934", options5);
        System.out.println("网络图片文字识别：" + res6.toString(2));
        //sample(client);

    }

    public static void sample(AipOcr client) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("probability", "true");


        // 参数为本地路径
        String image = "C:\\Users\\HL\\Desktop\\getCaptchaImage (2).jpg";
        JSONObject res = client.basicAccurateGeneral(image, options);
        System.out.println(res.toString(2));


        // 参数为二进制数组
        byte[] file = File2byte(image);
        res = client.basicAccurateGeneral(file, options);
        System.out.println(res.toString(2));
    }

    public static byte[] File2byte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    public static void byte2File(byte[] buf, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
